﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace WebeTerm.WebControls
{
    public abstract class ControlBase : System.Web.UI.UserControl
    {
        public Dictionary<string, object> Parameters { get; set; }

         
        protected string GetValue(Dictionary<string, object> ListInfo, string paraName)
        {
            if (ListInfo == null) return null;
            IEnumerable<KeyValuePair<string, object>> para = ListInfo.Where(it => it.Key == paraName);
            if (para.Count() == 0) return null;
            return Convert.ToString(para.FirstOrDefault().Value);
        }

        protected object GetObject(Dictionary<string, object> paras, string paraName)
        {
            if (paras == null) return null;
            KeyValuePair<string, object> para = paras.Where(it => it.Key == paraName).FirstOrDefault();
            return para.Value;
        }
         

        public string BuildDisplay(string text, string term)
        {
            string labelUpper = text.ToUpper();
            string termUpper = term.ToUpper();

            int start = labelUpper.IndexOf(termUpper);
            if (start >= 0)
            {
                string hit = text.Substring(start, term.Length);
                string display = Regex.Replace(text, term, string.Format("<span style=\"color:red\">{0}</span>", hit), RegexOptions.IgnoreCase);
                return display;
            }
            else
                return text;
        }
    }

}
